package red.mlz.gameservice.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import red.mlz.common.entity.game.Game;
import red.mlz.gameservice.config.RabbitMqConfig;
import red.mlz.gameservice.service.game.GameService;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class CategoryDeleteGameConsumer {

    @Autowired
    private GameService gameService;

    @RabbitListener(queues = RabbitMqConfig.CATEGORY_DELETE_GAME_QUEUE)
    public void handleCategoryDelete(Long categoryId) {

        try{
            //查询该分类下的游戏
            List<Game> games = gameService.findCategoryId(categoryId);
            if (games.isEmpty()){
                log.info("分类{}下无游戏，无需删除", categoryId);
            }

            //提取游戏列表，批量删除
            List<Long> gameIds = games.stream()
                    .map(Game::getId)
                    .collect(Collectors.toList());

            gameService.listDeletedByGameId(gameIds);

        }catch (Exception e){
            log.error("处理分类[{}]下游戏删除失败", categoryId, e);

            throw e;
        }
    }
}
